<?php
include 'admin_passport_module_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/jquery-ui/jquery-ui-1.10.1.custom.min.css' ),
    array ( 'path' => 'plugins/chosen-bootstrap/chosen/chosen.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/jquery-validation/dist/jquery.validate.min.js' ),
    array ( 'path' => 'plugins/chosen-bootstrap/chosen/chosen.jquery.min.js' ),
    array ( 'path' => 'scripts/nh-admin-passport-module-form.js' ),
);

$a_js_class[] = 'Module_Form.init();';
    
$a_pages = $a_module_actions = $a_actions = array ( );

if ($s_Action == 'update') {

    // <editor-fold defaultstate="collapsed" desc="Get Informations">
    if ($f_moduleid == '') { raise_error('No ID.'); }

    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_module);
    $db_query  = $db_query . " where moduleid = '$f_moduleid'";
    $db_result = db_query($db_conn, $db_query);

    if (!($d_passport_module = db_fetch($db_result))) { raise_error('Select None.'); }

    $db_query  = "select actionid from passport_module_action where moduleid = '$f_moduleid'";
    $db_result = db_query($db_conn, $db_query);
    
    while ($d_row = db_fetch($db_result)) {
        $a_module_actions[$d_row->actionid] = $d_row;
    }

    $db_query  = "select pageid from passport_module_page where moduleid = '$f_moduleid'";
    $db_result = db_query($db_conn, $db_query);
    
    while ($d_row = db_fetch($db_result)) {
        $d_row->pageid           = strtolower($d_row->pageid);
        $a_pages[$d_row->pageid] = $d_row->pageid;
    }
    // </editor-fold>
}

$a_files = glob('*.php');

// <editor-fold defaultstate="collapsed" desc="Get Data">
$db_query  = 'select actionid, actionname from passport_action where actionstatus = "ACTIVATED" order by actionname ';
$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    $a_actions[$d_row->actionid] = $d_row;
}
// </editor-fold>

include 'lib_head.php';
?>
<!-- BEGIN PAGE CONTENT-->
<div class="row-fluid">
    <div class="span12">
        <!-- BEGIN SAMPLE FORM PORTLET-->   
        <div class="portlet box blue">
            <div class="portlet-title">
                <div class="caption">
                    <i class="icon-plus"></i>
                    <span class="hidden-480">Module</span>
                </div>
            </div>
            <div class="portlet-body form">
                <!-- BEGIN FORM-->
                <form action="admin_passport_module_execute.php" method="post" id="form-module" class="form-horizontal">
                    <div class="alert alert-error hide">
                        <button class="close" data-dismiss="alert"></button>
                        Kiểm tra lại thông tin bên dưới.
                    </div>
                    <div class="control-group">
                        <label class="control-label">Module ID <span class="required">*</span></label>
                        <div class="controls">
                            <input type="text" name="moduleid" value="<?= check_plain($d_passport_module->moduleid) ?>" placeholder="Module ID" class="m-wrap" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Module Name <span class="required">*</span></label>
                        <div class="controls">
                            <input type="text" name="modulename" value="<?= check_plain($d_passport_module->modulename) ?>" placeholder="Module Name" class="m-wrap" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Module Status <span class="required">*</span></label>
                        <div class="controls">
                            <select name="modulestatus" class="m-wrap" tabindex="1">
                                <?php foreach($a_modulestatuses as $v_key){ ?>
                                <option value="<?= $v_key ?>" <?= $v_key == $d_passport_module->modulestatus ? ' selected="selected"' : ''?>><?= check_plain($v_key) ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Action <span class="required">*</span></label>
                        <div class="controls">
                            <select id="actionids" data-placeholder="Select your action..." class="chosen span6" multiple="multiple" tabindex="6">
                            <?php foreach ($a_actions as $o_item) { ?>
                                <option <?= isset($a_module_actions[$o_item->actionid]) ? 'selected' : ''?> value="<?=$o_item->actionid?> "><?=$o_item->actionname?></option>
                            <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">File Pages <span class="required">*</span></label>
                        <div class="controls">
                            <select id="pageids" data-placeholder="Select your files..." class="chosen span6" multiple="multiple" tabindex="6">
                            <?php foreach ($a_files as $v_key) { $v_key = str_replace('.php', '', $v_key); ?>
                                <option <?= isset($a_pages[$v_key]) ? 'selected' : ''?> value="<?=$v_key?> "><?=$v_key?></option>
                            <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Note</label>
                        <div class="controls">
                            <textarea name="note" class="large m-wrap" rows="3"><?= check_plain($d_passport_module->note) ?></textarea>
                        </div>
                    </div>
                    <div class="form-actions">
                        <?= $s_InputString ?>
                        <input type="hidden" name="action" id="action" value="<?= check_plain($s_Action) ?>"/>
                        <input type="hidden" name="pageids" id="action" value=""/>
                        <input type="hidden" name="actionids" id="action" value=""/>
                        <button type="button" class="btn">Cancel</button>
                        <button type="submit" class="btn blue"><i class="icon-ok"></i> Save</button>
                    </div>
                </form>
                <!-- END FORM-->  
            </div>
        </div>
        <!-- END SAMPLE FORM PORTLET-->
    </div>
</div>
<!-- END PAGE CONTENT-->
<?php include 'lib_tail.php'; ?>